<?php

namespace app\services;

use plugin\admin\app\model\Goods;
use plugin\admin\app\model\GoodsSKU;
use plugin\admin\app\model\OrderGoods;

class StockService
{
    public static function reduce($order)
    {
        $goodsList = OrderGoods::where('order_no', $order['order_no'])->get()->toArray();

        foreach ($goodsList as $goods) {
            if (!empty($goods['sku_id'])) {
                $skuInfo = GoodsSKU::where('id', $goods['sku_id'])->first();
                Goods::where('id', $skuInfo['goods_id'])->decrement('stock', $goods['count']);
                GoodsSKU::where('id', $goods['sku_id'])->decrement('stock', $goods['count']);
            } else {
                Goods::where('id', $goods['goods_id'])->decrement('stock', $goods['count']);
            }
        }

    }
}